home *** CD-ROM | disk | FTP | other *** search
- /*##########################################################################
- Copyright 2009 Tim Reid
-
- This file is part of the Stack Overflow Reputation Display extension
- for Mozilla Firefox.
-
- Stack Overflow Reputation Display is free software: you can
- redistribute it and/or modify it under the terms of the GNU General
- Public License as published by the Free Software Foundation, either
- version 3 of the License, or (at your option) any later version.
-
- Stack Overflow Reputation Display is distributed in the hope that it
- will be useful, but WITHOUT ANY WARRANTY; without even the implied
- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- See the GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public
- License along with Stack Overflow Reputation Display. If not,
- see <http://www.gnu.org/licenses/>.
- ##########################################################################*/
-
- var SOSite = function (init) {
- if (typeof init == "object") {
- for (var k in init)
- this[k] = init[k];
- } else if (typeof init == "string") {
- for (var i=0; i<SOSite.siteinfo.length; i+=2) {
- var tag = SOSite.siteinfo[i];
- if (init == tag) {
- var info = SOSite.siteinfo[i+1];
- for (var k in info)
- this[k] = info[k];
- this.sitetag = tag;
- break;
- }
- }
- }
-
- return this;
- };
-
- SOSite.siteinfo = [
- "so", {
- title: "Stack Overflow",
- uribase: "http://stackoverflow.com/",
- // smallicon: "http://sstatic.net/sf/favicon.ico",
- smallicon: "chrome://sorepdisplay/skin/sofavicon.ico",
- },
-
- "sf", {
- title: "Server Fault",
- uribase: "http://serverfault.com/",
- smallicon: "http://sstatic.net/sf/favicon.ico",
- },
-
- "su", {
- title: "Super User",
- uribase: "http://superuser.com/",
- smallicon: "http://sstatic.net/su/favicon.ico",
- },
-
- "meta", {
- title: "Meta Stack Overflow",
- uribase: "http://meta.stackoverflow.com/",
- smallicon: "http://sstatic.net/mso/favicon.ico",
- },
-
- "doctype", {
- title: "Doctype",
- uribase: "http://doctype.com/",
- smallicon: "http://doctype.com/images/favicon.png",
- },
-
- "onstartups", {
- title: "OnStartups Answers",
- uribase: "http://answers.onstartups.com/",
- smallicon: "http://answers.onstartups.com/theme/image/theme.favicon.0",
- },
-
- "epic", {
- title: "EpicAdvice.com",
- uribase: "http://epicadvice.com/",
- smallicon: "http://epicadvice.com/theme/image/theme.favicon.0",
- },
-
- "startups", {
- title: "Startups.com",
- uribase: "http://startups.com/",
- smallicon: "http://startups.com/theme/image/theme.favicon.0",
- },
-
- "moms4mom", {
- title: "moms4mom.com",
- uribase: "http://moms4mom.com/",
- smallicon: "http://moms4mom.com/theme/image/theme.favicon.0",
- },
-
- "fogbugz", {
- title: "FogBugz Knowledge Exchange",
- uribase: "http://fogbugz.stackexchange.com/",
- smallicon: "http://fogbugz.stackexchange.com/theme/image/theme.favicon.0",
- },
-
- "mathoverflow", {
- title: "Math Overflow",
- uribase: "http://mathoverflow.net/",
- smallicon: "http://mathoverflow.net/theme/image/theme.favicon.0",
- },
-
- "metase", {
- title: "Meta Stack Exchange",
- uribase: "http://meta.stackexchange.com/",
- smallicon: "http://meta.stackexchange.com/theme/image/theme.favicon.0",
- },
- ];
-
- SOSite.prototype = {
- title: "Stack Overflow",
- uribase: "http://stackoverflow.com/",
- smallicon: "chrome://sorepdisplay/skin/sofavicon.ico",
- sitetag: "so",
-
- pages: {
- main: { description: "main page",
- uripart: "", },
- questions: { description: "recent questions",
- uripart: "questions?sort=newest", },
- profile: { description: "my profile",
- uripart: "users/%u", },
- recent: { description: "my activity",
- uripart: "users/recent/%u", },
- activity: { description: "recent activity on my posts",
- uripart: "users/%u?tab=activity", },
- data: { description: "data source",
- uripart: "users/flair/%u.json", },
- },
-
- intervals: [
- { value: 300, description: "five minutes", },
- { value: 900, description: "fifteen minutes", },
- { value: 1800, description: "half hour", },
- { value: 3600, description: "hour", },
- ],
-
- badges: [
- "gold",
- "silver",
- "bronze"
- ],
-
- bookmarks: [
- "main",
- "questions",
- "profile",
- "recent",
- "activity",
- ],
-
- uripart: function (page) { return this.pages[page].uripart; },
- uri: function (page, user) {
- var part = this.uripart(page);
- if (typeof part != "string") return null;
-
- var uri = this.uribase + part;
- if (uri.match(/%u/) && !user)
- return null
-
- return uri.replace(/%u/g, user);
- },
- getdatauri: function (user) { return this.uri("data", user); },
-
- extractre: {
- reputation: new RegExp('"reputation":"([\\d,]+)"'),
- gold: new RegExp('title=\\\\"(\\d+) gold badges?\\\\"'),
- silver: new RegExp('title=\\\\"(\\d+) silver badges?\\\\"'),
- bronze: new RegExp('title=\\\\"(\\d+) bronze badges?\\\\"'),
- avatar: new RegExp('"gravatarHtml":"\\\\u003cimg src=\\\\"(http:\\/\\/www\\.gravatar\\.com\\/avatar\\/[0-9a-f]+\\?s=50&d=identicon&r=PG)\\\\"'),
- name: new RegExp('"displayName":"(.*?)"'),
- },
-
- extractinfo: function (text) {
- var info = {};
- for (var key in this.extractre) {
- var m = text.match(this.extractre[key]);
- if (m) {
- info[key] = m[1].replace(/&/g, "&");
- if (info[key].match(/^\d[\d,]*$/))
- info[key] = parseInt(info[key].replace(/,/g, ""));
- }
- }
-
- return info;
- },
-
- finduserdata: {
- xpath: '//div[@id="hlinks"]/a/@href',
- extract: new RegExp (/^\/users\/([0-9]+)\//),
- },
-
- finduser: function (doc) {
- if (!doc ||
- !doc.URL ||
- doc.URL.indexOf(this.uribase) != 0)
- return "";
-
- var r = doc.evaluate(this.finduserdata.xpath,
- doc,
- null,
- XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
- null);
- for (var i=0; i<r.snapshotLength; i++) {
- var node = r.snapshotItem(i);
- var m = node.nodeValue.match(this.finduserdata.extract);
- if (m) return m[1];
- }
- },
- };
-